home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / native1.1 / implementing / example-1.1 / handleError.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-13  |  2.2 KB  |  71 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include "MyTest.h"
  21.  
  22. jint Java_MyTest_handleError(JNIEnv *env, jobject obj, jint ival) {
  23.     jint ret, testInt;
  24.     jclass myClass, cls;
  25.     jfieldID fld, fld2, fld3;
  26.     jdouble retDbl;
  27.     jfloat retFlt;
  28.     jmethodID mid;
  29.     jstring msg;
  30.     jobject eobj;
  31.  
  32.     ret = 0;
  33.     myClass = (*env)->GetObjectClass(env, obj);
  34.     if (myClass == 0) return 1;
  35.     
  36.     // get fieldID
  37.     fld = (*env)->GetFieldID(env, myClass, "myInt","I");
  38.     if ((*env)->ExceptionOccurred(env)) {
  39.         printf("Caught exception.\n");
  40.         return 2;
  41.     }
  42.     // get field value
  43.     ret =  (*env)->GetIntField(env, obj, fld);
  44.     if ((*env)->ExceptionOccurred(env)) return 3;
  45.  
  46.     printf("Current value of myInt: %d\n", ret);
  47.     ret = ival;
  48.  
  49.     //check that new value is within limits
  50.     if (ival <0 || ival > 99) {
  51.        cls = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
  52.        if (cls == 0) return;
  53.        mid = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V");
  54.        if ((*env)->ExceptionOccurred(env)) return;
  55.        printf("Construct new string msg \n");
  56.        msg = (*env)->NewStringUTF(env, "Special error");
  57.        if ((*env)->ExceptionOccurred(env)) return;
  58.        printf("Construct new object.\n");
  59.        eobj = (*env)->NewObject(env, cls, mid, msg);
  60.        if ((*env)->ExceptionOccurred(env)) return;
  61.        (*env)->Throw(env, eobj);
  62.     }
  63.        
  64.      (*env)->SetIntField(env, obj, fld, ret);
  65.     if ((*env)->ExceptionOccurred(env)) return 4;
  66.     printf("New value of myInt: %d\n", ret);    
  67.  
  68.     return(0);
  69. }
  70.  
  71.